# -*- coding: utf-8 -*-
# @Time    : 2019/4/26 15:04
# @Author  : zjj
# @Email   : 1933860854@qq.com
# @File    : carSpider.py
# @Software: PyCharm
import scrapy
import requests
import time
class car_image(scrapy.Spider):
    name = 'car_image'
    allowed_domains = ['car.bitauto.com/']
    start_urls = ['http://car.bitauto.com/']

    def parse(self, response):
        list_con = response.xpath("//div[@class='tree-list']/ul[@class='list-con']/li[re:match(@id, '^letter\d+$')]")
        for list in list_con:
            # //*[@id="letter1"]/ul/li[1]/a[1]/div/span
            resutl = list.xpath(".//ul[@class='brand-list']/li")
            for res in resutl:
                name = res.xpath(".//a/div/span/text()").extract()[0]
                img = res.xpath(".//a/span/img/@src").extract()[0]
                try:
                    pic = requests.get(img, timeout=3)
                    file_name = "E:\\repo\\car\\{0}.png".format(name)
                    fp = open(file_name, 'wb')
                    fp.write(pic.content)
                    fp.close()
                    print(name + ' -> ' + img)
                    time.sleep(1)
                except Exception as e:
                    print(e)
                    print(name+" -> 下载失败")


if __name__ == '__main__':
    from scrapy import cmdline
    cmdline.execute("scrapy crawl car_image".split())
